package Test;

import domain.Student;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentManage {
    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<>();
        System.out.println("欢迎进入清华大学学生管理系统！ 我是你的助手小娜同学");
        while (true) {
            Scanner sc = new Scanner(System.in);
            System.out.println("1: 增添学生信息 , 2: 修改学生信息 , 3：删除学生信息,4：查看学生信息,5:退出系统,6:查找学生");
            int num = sc.nextInt();
            if (num == 1) {
                System.out.println("欢迎进入增添学生信息页面");
                addMethod(list);
            } else if (num == 2) {
                System.out.println("欢迎进入修改学生信息页面");
                setMethod(list);
            } else if (num == 3) {
                System.out.println("欢迎进入删除学生信息页面");
                deleteMethod(list);
            } else if (num == 4) {
                System.out.println("欢迎进入查看学生信息页面");
                lookMethod(list);
            } else if(num==5){
                System.out.println("感谢使用！再见");
                break;
            }else if(num==6){
                System.out.println("欢迎进入查找学生的界面");
                ChanZhaoStudent(list);
            }
            else {
                System.out.println("您输入的数字有误请重新输入");
            }
        }
    }

    private static void lookMethod(ArrayList<Student> list) {
        System.out.println("ID"+"   "+"姓名"+"    "+"年纪"+"   "+"性别"+"   "+"家庭住址"+"   ");
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            System.out.println(stu.getId()+"   "+stu.getName()+"   "+stu.getAge()+"   "+stu.getSex()+"   "+stu.getZhuZhi());
        }
    }

    private static ArrayList<Student> deleteMethod(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
     loo:  while(true) {
         System.out.println("请输入你要删除的学生的学号");
         int newId = sc.nextInt();
         System.out.println("请输入你要删除的学生的姓名");
         String Newname = sc.next();
            for (int i = 0; i < list.size(); i++) {
                if (newId == list.get(i).getId()) {
                    if (Newname.equals(list.get(i).getName())) {
                        list.remove(i);
                        i--;
                        System.out.println("删除成功");
                        return list ;
                    } else {
                        System.out.println("你输入的不正确");
                        continue loo;
                    }
                } else {
                    System.out.println("你输入的学号不存在确");
                    continue loo ;
                }
            }
        }
    }

    private static void setMethod(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
     lo:   while(true) {
            for (int i = 0; i < list.size(); i++) {
                System.out.println("请输入你要修改的学生的学号");
                int oldId = sc.nextInt();
                System.out.println("请输入你要修改学生的姓名");
                String oldName = sc.next();
                Student stu = list.get(i);
                if (oldName.equals(stu.getName())) {
                    if (oldId == stu.getId()) {
                        list.remove(i);
                        System.out.println("请输入学生的新名字");
                        String newName = sc.next();
                        System.out.println("请输入学生的年纪");
                        int newAge ;
                        while(true){
                            newAge = sc.nextInt();
                            if(newAge<=120&&newAge>=0){
                                break;
                            }else{
                                System.out.println("您输入的年纪有误,请输入学生的年纪");
                            }
                        }
                        System.out.println("请输入学生的新ID");
                        int newId = sc.nextInt();
                        System.out.println("请输入学生的性别");
                        String newSex = sc.next();
                        System.out.println("请输入学生的住址");
                        String newDiZhi = sc.next();
                        Student stu1 = new Student(newName, newAge, newId, newSex, newDiZhi);
                        boolean bo = list.add(stu1);
                        if (bo == true) {
                            System.out.println("修改成功");
                            break lo;
                        } else {
                            System.out.println("修改失败, 请重新输入");
                            continue lo ;
                        }


                    }
                } else if (i == list.size() - 1 && !oldName.equals(stu.getName()) && oldId != stu.getId()) {
                    System.out.println("你输入学号或姓名不存在 请重新输入");
                    continue lo;
                }
            }
        }
    }

    public static ArrayList<Student> addMethod(ArrayList<Student> list){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学生的姓名");
        String name = sc.next();
        System.out.println("请输入学生的年纪");
        int age ;
        while(true){
           age = sc.nextInt();
            if(age<=120&&age>=0){
                break;
            }else{
                System.out.println("您输入的年纪有误,请输入学生的年纪");
            }
        }
        System.out.println("请输入学生的ID");
        int id;
       loo: while(true){
          id = sc.nextInt();
       if(list.size()==0){
           break loo;
       }
       for (int i = 0; i < list.size(); i++) {
            /*System.out.println("请输入学生的ID");
             id = sc.nextInt();*/
               Student stu = list.get(i);
               int oldId = stu.getId();
               if (id == oldId) {
                   System.out.println("ID已经存在,请重新输入学生的ID");
                   continue loo;
               }else{
                   break loo;
               }
           }
       }

        System.out.println("请输入学生的性别");
        String sex = sc.next();
        System.out.println("请输入学生的家庭住址");
        String zhuZhi = sc.next();
        Student stu = new Student(name,age,id,sex,zhuZhi);
        boolean bo = list.add(stu);
        if(bo==true){
            System.out.println("添加成功");
        }else{
            System.out.println("添加失败");
        }
            return list;
    }
    public static void ChanZhaoStudent(ArrayList<Student> list){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要查找的ID");
        while(true) {
            int newId = sc.nextInt();
            for (int i = 0; i < list.size(); i++) {
                Student stu = list.get(i);
                if (newId == stu.getId()) {
                    System.out.println("ID" + "   " + "姓名" + "    " + "年纪" + "   " + "性别" + "   " + "家庭住址" + "   ");
                    System.out.println(stu.getId() + "   " + stu.getName() + "   " + stu.getAge() + "   " + stu.getSex() + "   " + stu.getZhuZhi() + "   ");
                    return;
                } else {
                    System.out.println("您输入的ID不存在，请你重新输入ID");
                    continue;
                }
            }
        }
    }
}
